package cn.tedu.hellocharts;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;


import java.util.List;

/**
 * ViewPager控件加载线形图、饼状图、柱状图
 * 对ViewPager控件进行数据填充
 */
public class ViewPagerAdapter extends PagerAdapter {
    //选项卡页
    private List<View> mViewList;
    //标题
    private List<String> mTitleList;
    //有参构造函数
    public ViewPagerAdapter(List<View> mViewList, List<String> mTitleList) {
        this.mViewList = mViewList;
        this.mTitleList = mTitleList;
    }

    /**
     *
     * @return 页卡数
     */
    @Override
    public int getCount() {
        return mViewList.size();
    }

    /**
     * 官方推荐写法，查看页卡
     * @param view
     * @param o
     * @return
     */
    @Override
    public boolean isViewFromObject(View view, Object o) {
        return view == o;
    }

    /**
     *
     * @param container
     * @param position
     * @return
     */
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        //添加页卡
        container.addView(mViewList.get(position));
        return mViewList.get(position);
    }

    /**
     * 删除页卡
     * @param container
     * @param position
     * @param object
     */
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(mViewList.get(position));
    }

    /**
     * 获取页卡标题
     * @param position
     * @return
     */
    @Override
    public CharSequence getPageTitle(int position) {
        return mTitleList.get(position);
    }
}
